View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   */
43  package org.exolab.jms.messagemgr;
44  
45  import javax.naming.CompositeName;
46  import javax.naming.Context;
47  import javax.naming.InvalidNameException;
48  import javax.naming.NameAlreadyBoundException;
49  import javax.naming.NameNotFoundException;
50  import javax.naming.NamingException;
51  
52  import org.exolab.jms.client.JmsDestination;
53  
54  
55  /***
56   * This class provides helper methods to register destinations in JNDI
57   *
58   * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:50:43 $
59   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
60   */
61  class ContextHelper {
62  
63      /***
64       * Binds a name to an object, overwriting any existing binding.
65       * All intermediate contexts and the target context
66       * (that named by all but the terminal atomic component of the name)
67       * are created if they don't already exist.
68       *
69       * @param       context             the context to rebind to
70       * @param       name                a destination name,
71       *                                  possibly containing one or more '.'
72       * @param       destination         the destination to bind
73       * @throws      NamingException
74       */
75      public static void rebind(Context context, String name,
76                                JmsDestination destination)
77          throws NamingException {
78          CompositeName composite = getCompositeName(name);
79          Context subcontext = context;
80          String component = null;
81  
82          for (int i = 0; i < composite.size() - 1; i++) {
83              component = composite.get(i);
84              if (component.length() == 0) {
85                  throw new InvalidNameException("'" + name
86                                                 + "' is not a valid name");
87              }
88              Object object = null;
89              try {
90                  object = subcontext.lookup(component);
91                  if (!(object instanceof Context)) {
92                      String subname = "";
93                      for (int j = 0; j <= i; j++) {
94                          if (j > 0) {
95                              subname += ".";
96                          }
97                          subname += composite.get(j);
98                      }
99                      throw new NameAlreadyBoundException(
100                         "'" + subname + "' is already bound");
101                 } else {
102                     subcontext = (Context) object;
103                 }
104             } catch (NameNotFoundException exception) {
105                 subcontext = subcontext.createSubcontext(component);
106             }
107         }
108 
109         component = composite.get(composite.size() - 1);
110         if (component.length() == 0) {
111             throw new InvalidNameException("'" + name
112                                            + "' is not a valid name");
113         }
114 
115         try {
116             Object object = subcontext.lookup(component);
117             if (object instanceof Context) {
118                 throw new NameAlreadyBoundException("'" + name +
119                                                     "' is already bound");
120             }
121         } catch (NameNotFoundException ignore) {
122         } catch (NamingException exception) {
123         }
124 
125         subcontext.rebind(component, destination);
126     }
127 
128     /***
129      * Unbinds the named object. Removes the terminal atomic name in name
130      * from the target context - that named by all but the terminal atomic
131      * part of name.
132      *
133      * @param       context             the context to unbind from
134      * @param       name                a destination name, possibly
135      *                                  containing one or more '.'
136      * @throws      NamingException
137      */
138     public static void unbind(Context context, String name)
139         throws NamingException {
140         try {
141             Object object = context.lookup(name);
142             if (!(object instanceof JmsDestination)) {
143                 throw new NamingException(
144                     "Cannot unbind name='" + name
145                     + "': it does not refer to a Destination");
146             }
147             context.unbind(name);
148         } catch (NameNotFoundException ignore) {
149         }
150     }
151 
152     private static CompositeName getCompositeName(String name)
153         throws NamingException {
154         // Need to replace the dots in destination names with a '/' in
155         // order for it to be parsed by CompositeName
156         CompositeName composite = new CompositeName(name.replace('.', '/'));
157         if (composite.size() == 0) {
158             throw new InvalidNameException("'" + name
159                                            + "' is not a valid name");
160         }
161         return composite;
162     }
163 }